﻿using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {

    internal class OpenInMainContainerService : Service {
        public void Open(VisualElement pane) {
            (provider as FramePane).OpenInMainContainer(pane);
        }
        public void Close() {
            (provider as FramePane).CloseInMainContainer();
        }
    }

    internal class FramePane : NonContextPaneBase {

        private MenuBar menuBar;
        private VisualElement mainContainer;
        private EditingFramePane editingFramePane;
        private LauncherPane launcherPane;

        const float updateStep = 1f;
        float updateTimer = 0;

        public FramePane(AGEEnvironment ageEnv) : base(ageEnv) {

            menuBar = new MenuBar(this) {
                name = "menuBar",
                style = {
                    minHeight = 25,
                }
            };
            mainContainer = new VisualElement() {
                name = "mainContainer",
                style = {
                    flexGrow = 1, // 占据剩余全部空间
                }
            };

            this.RegisterCallback<AttachToPanelEvent>(evt => ApplyDarkStyle());
        }

        protected override void OnDrawUI() {
            ageEnv.serviceManager.AddService<OpenInMainContainerService>(this);
            Add(menuBar);
            Add(mainContainer);
            if (ageEnv.ageContext != null) {
                DrawEditor();
            } else {
                DrawLauncher();
            }
        }

        void DrawEditor() {
            menuBar.Init();
            //
            if (editingFramePane == null) {
                editingFramePane = new EditingFramePane(ageEnv);
            }
            editingFramePane.SetHandle(ageEnv.ageContext.GetRootHandle());
            editingFramePane.StretchToParentSize();
            mainContainer.Add(editingFramePane);
        }
        void DrawLauncher() {
            menuBar.Init();
            editingFramePane?.SetHandle(null);
            // launcher
            if (launcherPane == null) {
                launcherPane = new LauncherPane(this);
            }
            mainContainer.Add(launcherPane);
            launcherPane.StretchToParentSize();
            launcherPane.Init();
        }

        protected override void OnClearUI() {
            ageEnv.serviceManager.RemoveService<OpenInMainContainerService>(this);
            editingFramePane?.SetHandle(null);
            Clear();
        }

        protected override void OnUpdateUI() {
            updateTimer -= Time.deltaTime;

            if (updateTimer <= 0) {
                editingFramePane?.Update();
                launcherPane?.Update();
                updateTimer = updateStep;
            }
        }

        internal void OpenInMainContainer(VisualElement pane) {
            mainContainer.Clear();
            mainContainer.Add(pane);
            pane.StretchToParentSize();
        }
        internal void CloseInMainContainer() {
            mainContainer.Clear();
            if (ageEnv.ageContext != null) {
                mainContainer.Add(editingFramePane);
            } else {
                mainContainer.Add(launcherPane);
            }
        }

        void ApplyDarkStyle() {
            Type utilityType = Type.GetType("UnityEditor.UIElements.UIElementsEditorUtility, UnityEditor");
            MethodInfo forceDarkStyleMethod = utilityType.GetMethod(
                "ForceDarkStyleSheet",
                BindingFlags.Static | BindingFlags.NonPublic
            );
            forceDarkStyleMethod.Invoke(null, new object[] { this });
        }
    }
}
